<layout name="Qingtaoke/default" />
<style>
    a,a:hover{
        color: #fff;
        text-decoration: none;
    }

</style>
<div class="col-md-offset-1 col-md-10">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h5>搜索关键词</h5>
        </div>
        <div class="panel-body">
            <form action="{:U('Keywords/store')}" method="post">
                <div class="form-group">
                    <label for="apikey">关键词名:</label>
                    <input type="text" name="keyword" class="form-control" value="">
                </div>
                <div class="form-group">
                    <label for="v">排序:</label> <em>默认999</em>
                    <input type="text" name="order" class="form-control" value="999">
                </div>
                <div class="form-group">
                    <label for="t">是否热词:</label> <em>默认否</em>
                    <select name="is_show" class="form-control">
                        <option value="0" selected>否</option>
                        <option value="1">是</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="t">是否显示:</label> <em>默认是</em>
                    <select name="hot" class="form-control">
                        <option value="0">否</option>
                        <option value="1" selected>是</option>
                    </select>
                </div>
                <button type="submit" class="btn btn-primary">添加</button>
            </form>
        </div>
        <div class="panel-body">
            <table class="table table-bordered">
                <tr>
                    <td>id :</td>
                    <td>排序 :</td>
                    <td>关键词 :</td>
                    <td>是否热词 :</td>
                    <td>是否热词 :</td>
                </tr>

                <?php foreach($datas as $data):?>
                <tr>
                    <td class="ids"><?php echo $data['id'];?></td>
                    <td><?php echo $data['order']?></td>
                    <td><?php echo $data['keyword']?></td>
                    <td><?php echo $data['hot'] == 1? '是':'否';?></td>
                    <td>
                        <input type="hidden" name="show" class="show" value="<?php echo $data['is_show']?>">
                        <button class="btn btn-primary btn-sm"><a href="{:U('Keywords/edit',['id'=>$data['id']])}">修改</a></button>
                        <button type="submit" class="btn btn-sm btn-warning btn2"><?php echo  $data['is_show'] == 1 ? '显示' : '隐藏' ?></button>
                        <button type="submit" class="btn btn-sm btn-danger btn3">删除</button>
                    </td>
                </tr>
                <?php endforeach;?>
            </table>
        </div>
    </div>
</div>

<script>
    var btn2 = document.getElementsByClassName('btn2');
    var btn3 = document.getElementsByClassName('btn3');
    var ids = document.getElementsByClassName('ids');
    var shows = document.getElementsByClassName('show');
    for(var i=0;i< btn2.length;i++) {
        btn2[i].index = i;
        btn2[i].onclick = function() {
            for(var j=0;j<ids.length;j++) {
                ids[j].index = j;
                var id = ids[this.index].innerHTML;
            }
            for(var k=0;k<shows.length;k++) {
                shows[k].index = k;
                var show = shows[this.index].value;
            }
            $.ajax({
                type: "post",
                url: "{:U('Keywords/update')}",
                data: {"id": id,"show":show},
                success: function (data) {
                    window.location.reload();
                }
            });
        }
    }
    for(var i=0;i< btn3.length;i++) {
        btn3[i].index = i;
        btn3[i].onclick = function() {
            for(var j=0;j<ids.length;j++) {
                ids[j].index = j;
                var id = ids[this.index].innerHTML;
            }
            $.ajax({
                type: "post",
                url: "{:U('Keywords/destroy')}",
                data: {"id": id},
                success: function (data) {
                    window.location.reload();
                }
            });
        }
    }
</script>
